package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSON;
import com.cn.wanxi.result.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//         1.如果前端传递中文时，可以会出现乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        2.要得到前端的数据
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String enable = req.getParameter("enable");
        String hobby = req.getParameter("hobby");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        //        3.封装model
        UserModel userModel = new UserModel();
//        PageModel pageModel=new PageModel();
//        pageModel.setPage(page);
//        pageModel.setLimit(limit);
        userModel.setPage(Tool.pageToInt(page));
        userModel.setLimit(Tool.limitToInt(limit));
        userModel.setUsername(Tool.nullToString(username));
        userModel.setPhone(Tool.nullToString(phone));
        userModel.setSex(Tool.nullToString(sex));
        userModel.setEnable(Tool.stringToInt(enable));
        userModel.setHobby(Tool.nullToString(hobby));
        userModel.setStart(Tool.nullToString(start));
        userModel.setEnd(Tool.nullToString(end));
//        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
        //        5.得到统一的返回值对象
        ResultModel resultModel = userService.findAll(userModel);
        //       6.将数据返回给前端页面
        resp.getWriter().println(JSON.toJSONString(resultModel));
    }
}
